﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 5. Sorting an array means to arrange its elements in 
			increasing order. Write a script to sort an array. Use 
			the "selection sort" algorithm: Find the smallest 
			element, move it at the first position, find the 
			smallest from the rest, move it at the second 
			position, etc. 
			Hint: Use a second array  -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		var array = new Array();
		var number = parseInt(prompt("Enter number of elements"));
		if (!isNaN(number)) {
			// Enter the array of numbers
			for (var i = 0, j = 0; i < number; i++) {
				var n = parseInt(prompt("Enter element " + i));
				if (!isNaN(n)) {
					array[j] = n;
					j++;
				}
			}

			jsConsole.writeLine("You have entered: ");
			jsConsole.writeLine(array.join());
			jsConsole.writeLine("");

			// Do the sort
			for (var i = 0; i < array.length; i++) {
				for (var j = (i + 1) ; j < array.length; j++) {
					if (array[i] > array[j]) {
						var temp = array[j];
						array[j] = array[i];
						array[i] = temp;
					}
				}
			}

			// Print sorted array
			jsConsole.writeLine("Sorted array: ");
			jsConsole.writeLine(array.join());
		}
		else {
			jsConsole.writeLine("Wrong Input.");
		}
	</script>
</body>
</html>
